﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CarHelpDesk.Web.Models.Repositories;
using CarHelpDesk.Web.Models;

namespace CarHelpDesk.Web.Controllers
{
    public class AnswersController : Controller
    {
        private IAnswersRepository repo;

        public AnswersController()
        {
            repo = new CarHelpDesk.Web.Models.Repositories.LinqRepositories.AnswersRepository();
        }

        public AnswersController(IAnswersRepository aRepository)
        {
            repo = aRepository;
        }


        /// <summary>
        /// Lists answers attached to the specified question.
        /// </summary>
        /// <param name="questionID">The question ID.</param>
        /// <returns></returns>
        public ActionResult Index(int questionID)
        {            
            return View(repo.getAnswersByQuestionID(questionID));
        }       

        /// <summary>
        /// Creates a form for creating new answer, attached to specified Question
        /// </summary>
        /// <param name="questionID">The question ID.</param>
        /// <returns></returns>
        public ActionResult Create(int questionID)
        {
            ViewData["questionID"] = questionID;
            return View();
        }


        /// <summary>
        /// Creates the specified answer.
        /// </summary>
        /// <param name="answer">The answer.</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public ActionResult Create(Answer answer)
        {
            answer.CreateDate = DateTime.Now;
            answer.UpdateDate = DateTime.Now;
            answer.AuthorID = this.GetCurrentUserID();
            answer.QuestionID = Int32.Parse( Request.Form["questionID"]);

            repo.Create(answer);

            return PartialView("SingleAnswer", answer);
        }
        
        //
        // GET: /Answers/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Answers/Edit/5
        [Authorize]
        [HttpPost]
        public JsonResult MarkAsAccepted(int answerID)
        {
            MarkAsAcceptedJsonResult result = new MarkAsAcceptedJsonResult();
            result.answerID = answerID;
            try
            {
                Guid currUser = this.GetCurrentUserID();

                Answer answerToUpdated = repo.getAnswerByID(answerID);
                if (answerToUpdated.Question.aspnet_User.UserId != currUser)
                {
                    result.errorCode = 2;
                    result.message = "You cannot mark this answer as correct.";
                    return Json(result);
                }
                IQueryable<Answer> answers = repo.getAnswersByQuestionID(answerToUpdated.Question.ID);
                if (answers.Count(a => a.IsCorrect) > 0)
                {
                    result.errorCode = 1;
                    result.message = "There is already answer marked as correct for this question";
                    return Json(result);
                }
                
                //answerToUpdated.IsCorrect = true;
                //repo.Update(answerToUpdated);

                repo.MarkAnswerAsCorrect(answerToUpdated);

                result.errorCode = 0;
                result.message = answerID.ToString();

                return Json(result);
            }
            catch
            {
                result.errorCode = -1;
                result.message = "error";
                return Json(result);
            }
        }

        //
        // GET: /Answers/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Answers/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }

    public class MarkAsAcceptedJsonResult
    {
        public int errorCode;
        public string message;
        public int answerID;
    }
}
